<?php
/**
 * 公司
 * @date    2020-01-01
 * @author  kiro
 * @email   294843009@qq.com
 * @version 1.0
 */
namespace App\Http\Controllers\Api\V1;

use App\Models\Company;
use App\Models\CompanyDepartment;
use Illuminate\Http\Request;

class CompanyController extends ApiController
{
    // 公司列表
    public function list(Request $request)
    {
        $page = (int)$request->get('page', 1);
        $limit = (int)$request->get('limit', 10);
        $user_id = (int)$request->get('user_id');
        $offset = ($page - 1) * $limit;

        $total = Company::where('status', 1)
            ->where(function ($query) use ($request, $user_id) {
                $user_id AND $query->where('user_id', $user_id);
            })->count();
        $data = Company::where('status', 1)
            ->where(function ($query) use ($request, $user_id) {
                $user_id AND $query->where('user_id', $user_id);
            })
            ->offset($offset)
            ->limit($limit)
            ->orderBy('sort', 'ASC')
            ->orderBy('id', 'ASC')
            ->get();
        foreach ($data as &$value) {
            $value['name'] = htmlspecialchars_decode($value['name']);
            $value['desc'] = htmlspecialchars_decode($value['desc']);
            $value['thumb'] = dealUrl($value['thumb']);
            $images = array_filter(explode(',', $value['images']));
            foreach ($images as &$image) {
                $image = dealUrl($image);
            }
            $value['images'] = $images;
        }
        return $this->success('成功', [
            'total' => $total,
            'total_page' => ceil($total / $limit),
            'current_page' => $page,
            'list' => $data
        ]);
    }

    // 发布公司
    public function add(Request $request)
    {
        if (empty($this->getUserId())) {
            return $this->error('user_id不能为空');
        }
        if (empty($request->name)) {
            return $this->error('公司名称不能为空');
        }
        $model = new Company();
        $model->user_id = $this->getUserId();
        $request->name AND $model->name = htmlspecialchars($request->name);
        $request->type AND $model->type = $request->type;
        $request->thumb AND $model->thumb = $request->thumb;
        $request->tel AND $model->tel = $request->tel;
        $request->address AND $model->address = $request->address;
        $request->legal_person AND $model->legal_person = $request->legal_person;
        $request->desc AND $model->desc = htmlspecialchars($request->desc);
        $request->sort AND $model->sort = $request->sort;
        if (!empty($request->images) && count($request->images) && is_array($request->images)) {
            $model->images = implode(',', $request->images);
        }
        $model->status = 1;
        $result = $model->save();
        if (!$result) {
            return $this->error('发布失败');
        }
        return $this->success('发布成功', $model->id);
    }

    // 部门树状列表
    public function departmentList(Request $request)
    {
        if (empty($request->company_id)) {
            return $this->error('公司ID不能为空');
        }
        $list = CompanyDepartment::getList($request->company_id);
        return $this->success('成功', $list);
    }

    // 发布部门
    public function departmentSave(Request $request)
    {
        if (empty($request->name)) {
            return $this->error('名称不能为空');
        }
        if ($request->id) {
            $model = CompanyDepartment::find($request->id);
        } else {
            $model = new CompanyDepartment();
        }
        $request->name AND $model->name = htmlspecialchars($request->name);
        $request->company_id AND $model->company_id = $request->company_id;
        $request->pid AND $model->pid = $request->pid;
        $request->sort AND $model->tel = $request->sort;
        $model->status = 1;
        $result = $model->save();
        if (!$result) {
            return $this->error('保存失败');
        }
        return $this->success('保存成功', $model->id);
    }

    // 删除部门
    public function departmentDel(Request $request)
    {
        $id = (int)$request->id;
        $res = CompanyDepartment::where('id', $id)->delete();
        if (!$res) {
            return $this->error('删除失败');
        }
        return $this->success('删除成功');
    }
}
